package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;


public class BibliotecaApp {

    private Menu menu;
    private PrintStream printStream;
    private BookLibrary bookLibrary;
    private MovieLibrary movieLibrary;
    private UserLibrary userLibrary;

    public BibliotecaApp(PrintStream printStream, BookLibrary bookLibrary, MovieLibrary movieLibrary, Menu menu, UserLibrary userLibrary) {
        menu.setApp(this);
        this.menu = menu;
        this.printStream = printStream;
        this.bookLibrary = bookLibrary;
        this.movieLibrary = movieLibrary;
        this.userLibrary = userLibrary;
    }

    public static void main(String[] args) throws IOException {
        PrintStream printStream = new PrintStream(System.out);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        Menu menu = new Menu(bufferedReader, printStream);
        HashMap<String, Book> books = intiliazeBooks();
        HashMap<String, Movie> movies = intiliazeMovies();
        HashMap<String, User> users = intiliazeUsers();
        new BibliotecaApp(printStream, new BookLibrary(books,printStream), new MovieLibrary(movies,printStream), menu, new UserLibrary(users, printStream)).start();
    }

    private static HashMap<String, User> intiliazeUsers() {
        HashMap<String, User> users = new HashMap<String, User>();
        User u0 = new User("111-1111", "1");
        User u1 = new User("111-1112", "2");
        users.put(u0.getUserLibraryNumber(), u0);
        users.put(u1.getUserLibraryNumber(), u1);
        return users;
    }

    public static HashMap<String, Movie> intiliazeMovies() {
        HashMap<String, Movie> movies = new HashMap<String, Movie>();
        Movie movie ;
        for(int i = 0; i < 15; i++){
            movie =  new Movie("Movie " + (i + 1), 1998, "Director " + (i + 1), 10);
            movies.put(movie.getName(), movie);
        }

        return movies;
    }

    public static HashMap<String, Book> intiliazeBooks() {
        HashMap<String, Book> books = new HashMap<String, Book>();

        String book1 = "Book 1";
        books.put(book1, new Book(book1));
        String book2 = "Book 2";
        books.put(book2, new Book(book2));
        return books;
    }

    public void start() throws IOException {
        menu.welcome();
        menu.showMenuOptions();
        menu.processMenuOption(menu.readMenuOption());
    }

    public void showBooks() {
       bookLibrary.showBooks();
    }

    public void reserveBook(String bookName) {
        bookLibrary.reserveBook(bookName);
    }


    public void showMovies() {
        movieLibrary.showHeader();
        movieLibrary.showMovies();

    }

    public void checkMembership() {
        printStream.println("Please talk to a librarian. Thank You.");
    }

    public void verifyUser(String userName, String Password) {
        userLibrary.verifyUser(userName,Password);
    }
}
